package state.component;

import state.Clip;
import state.GameConstants;
import state.Graphics;
import state.Key;
import state.Manager;
import state.Sprite;
import state.font.ImageText;

public class BottomBar extends Component {
  private String leftLabel = null;

  private String centerLabel = null;

  private String rightLabel = null;

  private int choosedMenuIndex = -1;

  private GameMenu leftMenu = null;
  private GameMenu rightMenu = null;

  private Clip reuseClip = new Clip(0, 0, 0, 0);

  public BottomBar(Manager manager1, int height) {
    super(manager1, 0, GameConstants.SCREEN_HEIGHT - height, GameConstants.SCREEN_WIDTH, height);
    manager1.setBottomBar(this);
    this._manager = manager1;
    setPosition(0, GameConstants.SCREEN_HEIGHT - height);
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    if (_manager == null) {
      System.out.println("Warning: need put manager to BottomBar to process action");
      return;
    }
    _manager.stopEvent();

    switch (choosedMenuIndex) {
    case 0:
      Key.isCommandKey = true;
      _manager.getParent().doKeyReleased(Key.SOFT_LEFT);
      break;
    case 1:
      Key.isCommandKey = true;
      _manager.getParent().doKeyReleased(Key.FIRE);
      break;
    case 2:
      Key.isCommandKey = true;
      _manager.getParent().doKeyReleased(Key.SOFT_RIGHT);
      break;
    }
    choosedMenuIndex = -1;
  }

  public void pointerPressed(Sprite source, int x1, int y1) {
    if (_manager == null) {
      System.out.println("Warning: need put manager to BottomBar to process action");
      return;
    }
    _manager.stopEvent();
    choosedMenuIndex = x1 / (GameConstants.SCREEN_WIDTH / 3);

    switch (choosedMenuIndex) {
    case 0:
      _manager.getParent().doKeyPressed(Key.SOFT_LEFT);
      break;
    case 1:
      _manager.getParent().doKeyPressed(Key.FIRE);
      break;
    case 2:
      _manager.getParent().doKeyPressed(Key.SOFT_RIGHT);
      break;
    }
  }

  public void setManager(Manager manager1) {
    manager1.setBottomBar(this);
    this._manager = manager1;
  }

  public void draw(Graphics g) {
    Clip.getClip1(g, reuseClip);
    g.setClip(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);

    if (_fillBackGround) {
      g.setColor(_backgroundColor);
      g.fillRect(_realX, _realY, _width, _height);

      if (_focusable) {
        if (choosedMenuIndex > -1) {
          g.setColor(_focusBgColor);
          g.fillRect(_realX + choosedMenuIndex * (_width / 3), _realY, _width / 3, _height);
        }
      }
    }

    if (_hasBorder) {
      g.setColor(borderColor);
      g.drawRoundRect(_realX, _realY, _width, _height, 5, 5);
    }

    if (_image != null) {
      g.drawImage(_image, _realX, _realY, GameConstants.TOP_LEFT_ANCHOR);
    }

    ImageText font = getFont();

    // Draw label and menu in left side
    if (leftLabel != null) {
      String labelTmp = leftLabel;
      if ((leftMenu != null) && (leftMenu.isShowing())) {
        leftMenu.draw(g);
        labelTmp = "Đóng";
      }

      if (choosedMenuIndex == 0) {
        font.setBold(true);
        if (_focusable) {
          font.drawString2(g, labelTmp, _focusFgColor, _realX + 5, _realY + _height - 5, GameConstants.BOTTOM_LEFT_ANCHOR);
        } else {
          font.drawString2(g, labelTmp, _forceGroundColor, _realX + 5, _realY + _height - 5, GameConstants.BOTTOM_LEFT_ANCHOR);
        }
        font.setBold(false);
      } else {
        font.drawString2(g, labelTmp, _forceGroundColor, _realX + 5, _realY + _height - 5, GameConstants.BOTTOM_LEFT_ANCHOR);
      }
    }

    // Draw label center side
    if (centerLabel != null) {
      if (choosedMenuIndex == 1) {
        font.setBold(true);
        if (_focusable) {
          font.drawString2(g, centerLabel, _focusFgColor, _realX + _width / 2, _realY + _height - 5, GameConstants.BOTTOM_HCENTER_ANCHOR);
        } else {
          font.drawString2(g, centerLabel, _forceGroundColor, _realX + _width / 2, _realY + _height - 5, GameConstants.BOTTOM_HCENTER_ANCHOR);
        }
        font.setBold(false);
      } else {
        font.drawString2(g, centerLabel, _forceGroundColor, _realX + _width / 2, _realY + _height - 5, GameConstants.BOTTOM_HCENTER_ANCHOR);
      }
    }

    // Draw label and menu in right side
    if (rightLabel != null) {
      String labelTmp = rightLabel;
      if ((rightMenu != null) && (rightMenu.isShowing())) {
        rightMenu.draw(g);
        labelTmp = "Đóng";
      }

      if (choosedMenuIndex == 2) {
        font.setBold(true);
        if (_focusable) {
          font.drawString2(g, labelTmp, _focusFgColor, _width - 5, _realY + _height - 5, GameConstants.BOTTOM_RIGHT_ANCHOR);
        } else {
          font.drawString2(g, labelTmp, _forceGroundColor, _width - 5, _realY + _height - 5, GameConstants.BOTTOM_RIGHT_ANCHOR);
        }
        font.setBold(false);
      } else {
        font.drawString2(g, labelTmp, _forceGroundColor, _width - 5, _realY + _height - 5, GameConstants.BOTTOM_RIGHT_ANCHOR);
      }
    }

    Clip.setClip(g, reuseClip);
  }

  public void reset() {
    leftLabel = null;
    centerLabel = null;
    rightLabel = null;
    leftMenu = null;
    rightMenu = null;
  }

  public void keyReleased(Sprite source, int keyCode) {
    // Kiểm tra hiển thị menu bên trái
    if ((keyCode == Key.SOFT_LEFT) && (leftMenu != null)) {
      if (leftMenu.isShowing()) {
        leftMenu.hide();
      } else {
        leftMenu.show();
      }
      return;
    }

    // Kiểm tra hiển thị menu bên phải
    if ((keyCode == Key.SOFT_RIGHT) && (rightMenu != null)) {
      if (rightMenu.isShowing()) {
        rightMenu.hide();
      } else {
        rightMenu.show();
      }
      return;
    }
  }

  public void setLeftMenu(String leftLabel1, GameMenu menu) {
    this.leftLabel = leftLabel1;
    this.leftMenu = menu;
    if (menu != null) {
      leftMenu.setPosition(leftMenu.getMenuX(), getRealY() - leftMenu.itemHeight);
      leftMenu.setBottomBar(this);
    }
  }

  public void setRightMenu(String rightLabel1, GameMenu menu) {
    this.rightLabel = rightLabel1;
    this.rightMenu = menu;
    if (menu != null) {
      rightMenu.setPosition(rightMenu.getMenuX(), getRealY() - rightMenu.itemHeight);
      rightMenu.setBottomBar(this);
    }
  }

  public GameMenu getLeftMenu() {
    return leftMenu;
  }

  public GameMenu getRightMenu() {
    return rightMenu;
  }

  public boolean isMenuShowing() {
    return (leftMenu != null && leftMenu.isShowing()) || (rightMenu != null && rightMenu.isShowing());
  }

  public void setLabels(String leftLabel1, String centerLabel1, String rightLabel1) {
    setLeftLabel(leftLabel1);
    setCenterLabel(centerLabel1);
    setRightLabel(rightLabel1);
  }

  public String getLeftLabel() {
    return leftLabel;
  }

  public void setLeftLabel(String leftLabel1) {
    this.leftLabel = leftLabel1;
  }

  public String getCenterLabel() {
    return centerLabel;
  }

  public void setCenterLabel(String centerLabel1) {
    this.centerLabel = centerLabel1;
  }

  public String getRightLabel() {
    return rightLabel;
  }

  public void setRightLabel(String rightLabel1) {
    this.rightLabel = rightLabel1;
  }
}
